package javaer.starter.placeholder.character;


import javaer.starter.constants.SymbolEnum;
import javaer.starter.placeholder.PlaceholderTokenParser;

/**
 * 添加带有项目名占位符的highway接口
 * 原始：/abc/xxx
 * 生成：/${rootArtifactId}/abc/xxx
 *
 * @author yangzhen
 * @date 2021/11/29 14:39
 * @since 1.0.0
 */
public class MapperScanTokenHandler extends PlaceholderTokenParser {

    @Override
    public String handleToken(String content) {

        String[] values = content.split(SymbolEnum.COMMA.getSymbol());

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(SymbolEnum.MAPPER_SCAN_OPEN_TOKEN.getSymbol());
        int index = 0;
        for (String value : values) {
            value = value.trim();
            if (value.startsWith(SymbolEnum.BASE_PACKAGES.getSymbol())) {
                value = SymbolEnum.MAPPER_BASE_PACKAGE.getSymbol();
            }
            if (index == 0) {
                stringBuilder.append(value);
            } else {
                stringBuilder
                        .append(SymbolEnum.COMMA.getSymbol())
                        .append(SymbolEnum.BLANK_SPACE.getSymbol())
                        .append(value);
            }
            index++;
        }
        stringBuilder.append(SymbolEnum.CLOSING_BRACKET.getSymbol());
        return stringBuilder.toString();
    }

    @Override
    public String getOpenToken() {
        return SymbolEnum.MAPPER_SCAN_OPEN_TOKEN.getSymbol();
    }

    @Override
    public String getCloseToken() {
        return SymbolEnum.CLOSING_BRACKET.getSymbol();
    }
}
